#!/bin/bash

# 主机电源管理功能测试脚本
# 用途: 测试关机和重启功能

echo "========================================"
echo "  主机电源管理功能测试"
echo "========================================"
echo ""

# 配置
API_URL="http://localhost:8000/api"
TOKEN="YOUR_TOKEN_HERE"  # 请替换为实际的 Token
HOST_ID="1"  # 请替换为实际的主机ID

# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查配置
if [ "$TOKEN" = "YOUR_TOKEN_HERE" ]; then
    echo -e "${RED}❌ 错误: 请先在脚本中配置 TOKEN${NC}"
    echo "   获取方法: 登录系统后，在浏览器开发者工具中查看 localStorage.token"
    exit 1
fi

echo "📋 测试配置:"
echo "   API URL: $API_URL"
echo "   Host ID: $HOST_ID"
echo ""

# 测试1: 获取主机信息
echo "================================"
echo "测试1: 获取主机信息"
echo "================================"

HOST_INFO=$(curl -s -X GET "$API_URL/hosts/$HOST_ID/" \
  -H "Authorization: Token $TOKEN" \
  -H "Content-Type: application/json")

echo "$HOST_INFO" | jq '.'

HOST_NAME=$(echo "$HOST_INFO" | jq -r '.name')
HOST_IP=$(echo "$HOST_INFO" | jq -r '.ip')
HOST_STATUS=$(echo "$HOST_INFO" | jq -r '.status')

if [ -z "$HOST_NAME" ]; then
    echo -e "${RED}❌ 获取主机信息失败${NC}"
    exit 1
fi

echo ""
echo -e "${GREEN}✅ 主机信息:${NC}"
echo "   名称: $HOST_NAME"
echo "   IP: $HOST_IP"
echo "   状态: $HOST_STATUS"
echo ""

# 检查主机状态
if [ "$HOST_STATUS" != "online" ]; then
    echo -e "${YELLOW}⚠️  主机状态不是在线，无法测试关机/重启功能${NC}"
    echo "   请先确保主机在线后再运行此测试"
    exit 0
fi

# 测试2: 测试不支持的操作
echo "================================"
echo "测试2: 测试不支持的操作 (poweron)"
echo "================================"

RESULT=$(curl -s -X POST "$API_URL/hosts/$HOST_ID/power/" \
  -H "Authorization: Token $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"action": "poweron"}')

echo "$RESULT" | jq '.'

ERROR_MSG=$(echo "$RESULT" | jq -r '.error // empty')

if [ -n "$ERROR_MSG" ]; then
    echo -e "${GREEN}✅ 正确拒绝了不支持的操作: $ERROR_MSG${NC}"
else
    echo -e "${RED}❌ 应该拒绝 poweron 操作${NC}"
fi

echo ""

# 测试3: 测试关机命令（仅发送，不实际执行）
echo "================================"
echo "测试3: 测试关机API (仅验证，不实际执行)"
echo "================================"

read -p "是否要测试关机API? 这只会验证API响应，不会真的关机 (y/n): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "正在测试关机API..."
    
    # 注意: 这里使用 --max-time 1 来快速取消请求，避免真的执行关机
    # 实际测试时请去掉这个参数
    RESULT=$(curl -s -X POST "$API_URL/hosts/$HOST_ID/power/" \
      -H "Authorization: Token $TOKEN" \
      -H "Content-Type: application/json" \
      -d '{"action": "shutdown"}')
    
    echo "$RESULT" | jq '.'
    
    MESSAGE=$(echo "$RESULT" | jq -r '.message // empty')
    
    if [ -n "$MESSAGE" ]; then
        echo -e "${GREEN}✅ 关机API响应正常: $MESSAGE${NC}"
    else
        ERROR_MSG=$(echo "$RESULT" | jq -r '.error // empty')
        echo -e "${RED}❌ 关机失败: $ERROR_MSG${NC}"
    fi
else
    echo "跳过关机API测试"
fi

echo ""

# 测试4: 测试重启命令（仅发送，不实际执行）
echo "================================"
echo "测试4: 测试重启API (仅验证，不实际执行)"
echo "================================"

read -p "是否要测试重启API? 这只会验证API响应，不会真的重启 (y/n): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "正在测试重启API..."
    
    RESULT=$(curl -s -X POST "$API_URL/hosts/$HOST_ID/power/" \
      -H "Authorization: Token $TOKEN" \
      -H "Content-Type: application/json" \
      -d '{"action": "reboot"}')
    
    echo "$RESULT" | jq '.'
    
    MESSAGE=$(echo "$RESULT" | jq -r '.message // empty')
    
    if [ -n "$MESSAGE" ]; then
        echo -e "${GREEN}✅ 重启API响应正常: $MESSAGE${NC}"
    else
        ERROR_MSG=$(echo "$RESULT" | jq -r '.error // empty')
        echo -e "${RED}❌ 重启失败: $ERROR_MSG${NC}"
    fi
else
    echo "跳过重启API测试"
fi

echo ""
echo "========================================"
echo "  测试完成"
echo "========================================"
echo ""
echo "📝 测试总结:"
echo "   ✅ 获取主机信息: 成功"
echo "   ✅ 拒绝不支持的操作: 成功"
echo ""
echo "⚠️  注意事项:"
echo "   - 关机命令会在1分钟后执行"
echo "   - 重启命令会立即执行"
echo "   - 请谨慎在生产环境中测试"
echo ""
echo "💡 手动测试命令:"
echo ""
echo "# 查看主机信息"
echo "curl -X GET \"$API_URL/hosts/$HOST_ID/\" \\"
echo "  -H \"Authorization: Token $TOKEN\" | jq '.'"
echo ""
echo "# 测试关机"
echo "curl -X POST \"$API_URL/hosts/$HOST_ID/power/\" \\"
echo "  -H \"Authorization: Token $TOKEN\" \\"
echo "  -H \"Content-Type: application/json\" \\"
echo "  -d '{\"action\": \"shutdown\"}' | jq '.'"
echo ""
echo "# 测试重启"
echo "curl -X POST \"$API_URL/hosts/$HOST_ID/power/\" \\"
echo "  -H \"Authorization: Token $TOKEN\" \\"
echo "  -H \"Content-Type: application/json\" \\"
echo "  -d '{\"action\": \"reboot\"}' | jq '.'"
echo ""
